package com.dlwx.parttimetalent.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextClock;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.bean.BaseBean;
import com.dlwx.parttimetalent.utils.SPUtils;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;

import org.androidannotations.annotations.App;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LockScreenActivity extends Activity {

    @BindView(R.id.clock)
    TextClock clock;
    @BindView(R.id.lock_title_tv)
    TextView lockTitleTv;
    @BindView(R.id.lock_time_tv)
    TextView lockTimeTv;
    @BindView(R.id.lock_msg_tv)
    TextView lockMsgTv;
    @BindView(R.id.unlock)
    TextView unlock;
    @BindView(R.id.activity_lock_screen_msg)
    RelativeLayout activityLockScreenMsg;
    private List<BaseBean> mDatas = new ArrayList<>();
    private PowerManager.WakeLock wakeLock;
    private boolean toMianActivity = false;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private App app;
    private int msgID;

    String type = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Window win = getWindow();
        win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        lightScreen();
        setContentView(R.layout.activity_lock_screen);
        ButterKnife.bind(this);
        app = (App) this.getApplication();


        SimpleDateFormat df = new SimpleDateFormat("MM-dd HH:mm");//设置日期格式
        lockTimeTv.setText(df.format(new Date()));
//        记录当前activity在前台
        SPUtils.put(this, "showScreenMsg", true);
    }

    /**
     * 点亮屏幕
     */
    private void lightScreen() {
        PowerManager pm = (PowerManager) this.getSystemService(this.POWER_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
            if (!pm.isInteractive()) {
                wakeLock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP
                        | PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "bright");
                wakeLock.acquire();
            }
        } else {
            if (!pm.isScreenOn()) {
                wakeLock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP |
                        PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "bright");
                wakeLock.acquire();
            }
        }
      /*  new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if(wakeLock.isHeld()){
                    wakeLock.release();
                }
            }
        },3000);*/
    }


    //Activity中复写OnNewIntent
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        lightScreen();
        getPushMsg(intent);
    }


    @Override
    protected void onResume() {
        super.onResume();
        getPushMsg(getIntent());
    }

    private void getPushMsg(Intent i) {
//        List<Message> msgs = helper.queryUnReadMsg();
//        Log.d("CCC", "size-lock:" + msgs.size());
//        if (!msgs.isEmpty()) {
//            Message item = msgs.get(msgs.size() - 1);
//            msgTv.setText(item.getContent());
//            msgID = item.getId();
//            type = item.getJumpType();
//
//        } else {
//            type = i.getStringExtra(Constant.JUMP);
//        }
//        setMsgTitle(type);
    }


    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (wakeLock == null) {
            return;
        }
        if (wakeLock.isHeld()) {
            wakeLock.release();
        }
        SPUtils.put(this, "showScreenMsg", false);
    }


    private void setMsgTitle(String type) {
//        if (TextUtils.equals(type, Constant.JUMP_SESSION)) {
//            lockTitleTv.setText("万利宝客服");
//        } else {
//            lockTitleTv.setText("万利宝");
//        }
    }

    @OnClick({R.id.unlock, R.id.lock_msg_tv})
    public void onClick(View view) {
        Intent i = new Intent();
        i.setClass(LockScreenActivity.this, MainActivity.class);
        switch (view.getId()) {
            case R.id.lock_msg_tv:
                startActivity(i);
                break;
            case R.id.unlock:
                LockScreenActivity.this.finish();
                break;
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
     /*   LogUtil.showLog("recycler:"+recycler.getY()+"-top:"+recycler.getHeight()+"-h:");
        LogUtil.showLog("dianji:"+ev.getY());*/
//        int click= (int) ev.getY();
//        int recyclerViewHeight=recycler.getHeight();
//        int recyclerTop= (int) recycler.getY();
//        if(click>=recyclerTop&&click<=(recyclerTop+recyclerViewHeight)){
//            this.getSwipeBackLayout().setEnableGesture(false);
//        }else{
//            this.getSwipeBackLayout().setEnableGesture(true);
//        }
        return super.dispatchTouchEvent(ev);
    }

}
